<?php

	namespace app\stream\model;

	class StreamShop extends \think\Model {
		//定义当前表
		protected $tableName = 'stream_shop';
		//定义表的字段信息
		protected $fields = ['id', 'uid', 'stream_id', 'shop_name', 'shop_cover', 'shop_link', 'shop_price', 'create_time', 'vid','is_del'];
		//定义需要填充的数据,调用validateData方法进行填充
		protected $auto = [];



		public function setError($message = '操作失败')
		{
			$this->error = $message;

			return false;
		}

		/**
		 * 储存单例的数据字段
		 *
		 * @var object
		 **/
		private static $_instance;

		/**
		 * 获取单例
		 *
		 * @return object
		 **/
		public static function getInstance()
		{
			if (!self::$_instance instanceof self) {
				self::$_instance = new self('stream\StreamShop');
			}

			return self::$_instance;
		}

		/**
		 * @name 本地数据验证(过滤和自动填充数据)
		 * @access protected
		 * @param array $data 验证的数据数组
		 * @param bool $sid
		 * @param boolean $auto 是否自动填充数据
		 * @return array|bool 验证后的数据
		 */
		protected function validateData($data = [], $sid = true, $auto = false)
		{
			if (!$data || !is_array($data)) {
				return false;
			}
			if ($sid && !$data['stream_id']) {
				return $this->setError('直播流参数错误');
			}
			if (!$data['shop_name']) {
				return $this->setError('商品名称不能为空');
			}
			if ($data['shop_cover'] < 1) {
				return $this->setError('商品封面图不能为空');
			}
			if (!$data['shop_link']) {
				return $this->setError('商品链接不能为空');
			}
			if (!preg_match("/shante\.me/i", $data['shop_link'])) {
				if (!model('open/UserData')->checkShopOuterChain($data['uid'])) {
					return $this->setError('商品链接地址不匹配');
				}
			}

			$res = [];
			$this->fields = $this->fields ?: $this->getFields();
			foreach ($data as $k => $v) {
				in_array($k, $this->fields) && $res[$k] = $v;
			}
			if ($auto && !empty($this->auto)) {
				return array_merge($res, $this->auto);
			}

			return $res;
		}

		/**
		 * @name 添加本地单条数据
		 * @access public
		 * @param array $data 添加的数据数组
		 * @return bool|mixed 数据表ID
		 */
		public function add($data = []) {
			if (is_string($data)) {
				$data = explode(',', $data);
			} else if (!is_array($data)) {
				return false;
			}
			$data = self::validateData($data, true);
			if (!$data) {
				return false;
			}
			return parent::add($data);
		}

		/**
		 * 更新回放ID
		 * @param $stream_id
		 * @param int $videoId
		 * @return bool
		 * @throws \think\Exception
		 */
		public function setVideo($stream_id, $videoId = 0)
		{
			$map['stream_id'] = $stream_id;
			$map['is_del'] = 0;
			$map['vid'] = 0;

			return $this->where($map)->order('id desc')->limit(1)->save(['vid' => $videoId]);
		}

		/**
		 * @name 根据直播流ID获取直播商品
		 * @param $streamId
		 * @return mixed
		 * @throws \think\Exception
		 */
		public function getShopByStreamId($streamId)
		{
			$map['stream_id'] = $streamId;
			$map['is_del'] = 0;
			$map['vid'] = 0;

			$data = $this->where($map)->order('id desc')->find();
			if (!$data) {
				return (object) [];
			}
			$data['shop_cover'] = (object) (model('attach/Attach', ['getAttachUrl', $data['shop_cover']])[0]);

			return $data;
		}


		/**
		 * @name 根据直播回放ID获取直播商品
		 * @param $video_id
		 * @return mixed
		 * @throws \think\Exception
		 */
		public function getShopByVideo($video_id)
		{
			$map['vid'] = $video_id;
			$map['is_del'] = 0;

			$data = $this->where($map)->find();
			if (!$data) {
				return (object) [];
			}
			$data['shop_cover'] = (object) (model('attach/Attach', ['getAttachUrl', $data['shop_cover']])[0]);

			return $data;
		}

		public function unStreamId($streamId)
		{
			$map['stream_id'] = $streamId;

			return $this->where($map)->save(['stream_id' => '']);
		}
	}
